package com.org.qrtrace.utils;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.org.qrtrace.pojo.Ptype;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.UUID;

public class CommonUtils {
    /*
    生成uuid
     */
    public static String getUUID(){
            return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static void setTimeAndUser(Object object, HttpServletRequest request) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        DecodedJWT token = JwtUtils.verifyToken(request.getHeader("token"));
        Method setCreateBy = object.getClass().getDeclaredMethod("setCreateBy", String.class);
        //如果setCreateBy方法存在，则通过Security的方法获取用户名并通过反射调用方法存入
        setCreateBy.invoke(object, token.getId());
        //如果setCreateTime方法存在，则获取现在的时间并通过反射调用方法存入
        Method setCreateTime = object.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class);
        setCreateTime.invoke(object, LocalDateTime.now());
    }
}
